package blog

import (
	kinit "longblog/work/base/initialize"
	kroute "longblog/work/base/route"
	kcode "longblog/work/code"
	kbase "longblog/work/control/base"
	kdaoblog "longblog/work/dao/blog"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

type ArticleType struct {
}

func NewArticleType() *ArticleType {
	return &ArticleType{}
}
func (h *ArticleType) Load() (route []kroute.RouteWrapStruct) {
	route = make([]kroute.RouteWrapStruct, 0)

	route = append(route, kbase.Wrap("GET", "/blog/articletype", h.indexHandle, kbase.MIDDLE_TYPE_CHECK_PERMISSION))
	route = append(route, kbase.Wrap("GETPOST", "/blog/articletype/add", h.addHandle, kbase.MIDDLE_TYPE_CHECK_PERMISSION))
	route = append(route, kbase.Wrap("GETPOST", "/blog/articletype/edit", h.editHandle, kbase.MIDDLE_TYPE_CHECK_PERMISSION))
	route = append(route, kbase.Wrap("POST", "/blog/articletype/del", h.delHandle, kbase.MIDDLE_TYPE_CHECK_PERMISSION))

	kbase.InsertMenu("/blog/articletype")
	kbase.InsertMenu("/blog/articletype/add")
	kbase.InsertMenu("/blog/articletype/edit")
	kbase.InsertMenu("/blog/articletype/del")
	return
}

type ArticleTypeForm struct {
	Id       int64  `form:"id"`
	TypeName string `form:"type_name" binding:"-"`
	LangType int64  `form:"lang_type" binding:"-"`
	SortNum  int64  `form:"sort_num"`
}

// 类别首页handle
func (h *ArticleType) indexHandle(c *gin.Context) {

	var param ArticleTypeForm
	if err := c.ShouldBind(&param); err != nil {
		kinit.LogError.Println(err)
		kbase.SendErrorTokenJsonStr(c, kcode.PARAM_WRONG, "")
		return
	}
	result, _ := kdaoblog.ObjArticleType.GetAllWithTitleOrLangType(nil, param.TypeName, param.LangType)

	g := make(gin.H)
	g["token"] = c.GetString("token")
	g["lists"] = result
	g["typeName"] = param.TypeName
	g["langType"] = param.LangType
	c.HTML(http.StatusOK, "blog/ArticleType/index.html", g)
}

// 添加类别handle
func (h *ArticleType) addHandle(c *gin.Context) {
	var param ArticleTypeForm
	if c.Request.Method == "POST" {
		if err := c.ShouldBind(&param); err != nil {
			kinit.LogError.Println(err)
			kbase.ReturnJson(c, http.StatusOK, kcode.PARAM_WRONG, nil)
			return
		}
		if _, err := kdaoblog.ObjArticleType.Insert(nil, param.LangType, param.TypeName); err != nil {
			kbase.ReturnJson(c, http.StatusOK, kcode.OPERATION_WRONG, nil)
			return
		}
		kbase.ReturnJson(c, http.StatusOK, kcode.SUCCESS_STATUS, nil)
		return
	}
	g := make(gin.H)
	g["token"] = c.GetString("token")
	c.HTML(http.StatusOK, "blog/ArticleType/add.html", g)
}

// 编辑
func (h *ArticleType) editHandle(c *gin.Context) {
	var param ArticleTypeForm
	if c.Request.Method == "POST" {
		if err := c.ShouldBind(&param); err != nil {
			kinit.LogError.Println(err)
			kbase.ReturnJson(c, http.StatusOK, kcode.PARAM_WRONG, nil)
			return
		}

		m := make(map[string]interface{}, 4)
		m["sort_num"] = param.SortNum
		m["lang_type"] = param.LangType
		m["type_name"] = param.TypeName
		if err := kdaoblog.ObjArticleType.UpdateMById(nil, param.Id, m); err != nil {
			kbase.ReturnJson(c, http.StatusOK, kcode.OPERATION_WRONG, nil)
			return
		}
		kbase.ReturnJson(c, http.StatusOK, kcode.SUCCESS_STATUS, nil)
		return
	}
	idStr, _ := c.GetQuery("id")
	id, _ := strconv.ParseInt(idStr, 10, 64)
	data, _ := kdaoblog.ObjArticleType.GetById(nil, id)
	g := make(gin.H)
	g["data"] = data
	g["token"] = c.GetString("token")
	c.HTML(http.StatusOK, "blog/ArticleType/edit.html", g)
}
func (h *ArticleType) delHandle(c *gin.Context) {
	idStr := c.PostForm("id")
	id, _ := strconv.ParseInt(idStr, 10, 64)

	if objs, _ := kdaoblog.ObjArticle.GetAllWith(nil, "", 0, id); len(objs) > 0 {
		kbase.ReturnJson(c, http.StatusOK, kcode.FAIL_DELETE_TYPE, nil)
		return
	}

	kdaoblog.ObjArticleType.DeleteById(nil, id)
	kbase.ReturnJson(c, http.StatusOK, kcode.SUCCESS_STATUS, nil)
}
